#!/bin/bash
# 读取主机信息文件，每行包含 IP 地址和密码  
while IFS= read -r line; do  
    ip=$(echo "$line" | awk '{print $1}')  
    password=$(echo "$line" | awk '{print $2}')  
  
    # 在目标主机上创建 SSH 目录并设置权限  
    sshpass -p "$password" ssh -o StrictHostKeyChecking=no "root@$ip" "mkdir -p ~/.ssh && chmod 700 ~/.ssh"  
  
    # 将公钥复制到目标主机  
    sshpass -p "$password" scp ~/.ssh/id_rsa.pub "root@$ip:~/.ssh/authorized_keys"  
done < "hosts.txt"